package com.web.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.web.dao.AccountMapper;
import com.web.dao.MemberMapper;
import com.web.dao.OriginalDetailMapper;
import com.web.domain.WebResponse;
import com.web.domain.ZtreeVo;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.domain.dataTable.BootstrapTableResponse;
import com.web.entity.Account;
import com.web.entity.Member;
import com.web.entity.OriginalDetail;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author FDjavaone
 * @date 2017/9/30
 */

@Slf4j
@Controller
public class WebMemberController {

    @Autowired
    private OriginalDetailMapper originalDetailMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "sysu/user",
            method = RequestMethod.GET)
    public String user(Model model, HttpServletRequest request) {
        return "member";
    }

    @RequestMapping(value = "sysu/memberTree",
            method = RequestMethod.GET)
    public String memberTree(Model model, HttpServletRequest request) {
        return "memberTree";
    }

    @RequestMapping(value = "sysu/orig",
            method = RequestMethod.GET)
    public String orig(Model model, HttpServletRequest request) {
        return "original";
    }


    @Data
    public static class MemberColumns extends BootstrapTableColumns {
        private String phone;
        private String name;
        private String memberType;
        private String identityNo;
        private Integer level;

    }


    @RequestMapping("/sysu/getMembers")
    @ResponseBody
    public BootstrapTableResponse getMskBuy(MemberColumns memberColumns) {
        PageHelper.startPage(memberColumns.getPageNumber(), memberColumns.getPageSize());
        Page<Member> page = memberMapper.selectAllMembers(memberColumns);
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }

    @RequestMapping("get/tree/members")
    @ResponseBody
    public List getTreeMenus() {
        MemberColumns memberColumns = new MemberColumns();
        List<Member> sysMenus = memberMapper.selectAllMembers(memberColumns);
        List<ZtreeVo> ztreeVos = Lists.newArrayList();
        if (sysMenus != null) {
            sysMenus.forEach(value -> {
                ZtreeVo ztreeVo = new ZtreeVo();
                ztreeVo.setId("" + value.getInviteCode());
                String username = "未实名";
                if (value.getName() != null && !"".equals(value.getName())) {
                    username = value.getName();
                }
                ztreeVo.setName(value.getPhone() + "[" + username + "]");
                ztreeVo.setpId("" + value.getInvitedCode());
                ztreeVos.add(ztreeVo);
            });
        }
        return ztreeVos;
    }


    @RequestMapping("/sysu/getOriginal")
    @ResponseBody
    public BootstrapTableResponse getOriginal(MemberColumns memberColumns) {
        memberColumns.setMemberType("original");
        PageHelper.startPage(memberColumns.getPageNumber(), memberColumns.getPageSize());
        Page<Member> page = memberMapper.selectAllMembers(memberColumns);
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        for (Member m : page) {
            Account account = accountMapper.selectByMid(m.getId());
            m.setBonusAmt(account.getOriginalIntegral());
        }
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sysu/rechargeOri")
    @ResponseBody
    public WebResponse rechargeOri(Member member) {
        Date date = new Date();
        Account account = accountMapper.selectByMid(member.getId());
        Member m = memberMapper.selectByPrimaryKey(member.getId());
        accountMapper.originalAdd(member.getBonusAmt(), account.getId());
        OriginalDetail originalDetail = new OriginalDetail();
        originalDetail.setaId(account.getId());
        originalDetail.setContent("获得积分");
        originalDetail.setDetail("+" + member.getBonusAmt());
        originalDetail.setBal(accountMapper.selectByMid(member.getId()).getOriginalIntegral().toString());
        originalDetail.setName(m.getName());
        originalDetail.setPhone(m.getPhone());
        originalDetail.setFromNum("后台");
        originalDetail.setModifyTime(date);
        originalDetail.setCreateTime(date);
        originalDetailMapper.insertSelective(originalDetail);

        return WebResponse.ok("操作成功");

    }


    @RequestMapping("/sysu/changeMemberType")
    @ResponseBody
    public WebResponse changeMemberType(Member member) {
        Date date = new Date();
        Member old = memberMapper.selectByPrimaryKey(member.getId());
        if (old.getMemberType().equals("common")) {
            member.setMemberType("original");
            member.setModifyTime(date);
            memberMapper.updateByPrimaryKeySelective(member);
            return WebResponse.ok("操作成功");
        } else {
            return WebResponse.fail("操作失败");
        }
    }


    @RequestMapping("sysu/getAccount")
    @ResponseBody
    public WebResponse getAccount(Member member) {
        Account account = accountMapper.selectByMid(member.getId());
        return WebResponse.ok("操作成功", account);
    }

    @RequestMapping("/sysu/memberForbidden")
    @ResponseBody
    public WebResponse memberForbidden(Member member) {
        //设置账户禁用，清除用户缓存token
        Date date = new Date();
        member.setEnabled(0);
        member.setModifyTime(date);
        memberMapper.updateByPrimaryKeySelective(member);

        Member old = memberMapper.selectByPrimaryKey(member.getId());
        String token = stringRedisTemplate.opsForValue().get(old.getPhone());

        //清除redis缓存
        stringRedisTemplate.delete(token);
        stringRedisTemplate.delete(old.getPhone());

        return WebResponse.ok("操作成功");
    }


    @RequestMapping("/sysu/memberPass")
    @ResponseBody
    public WebResponse memberPass(Member member) {
        Date date = new Date();
        member.setEnabled(1);
        member.setModifyTime(date);
        memberMapper.updateByPrimaryKeySelective(member);
        return WebResponse.ok("操作成功");
    }

}
